//
//  TemperatureMeter.swift
//  CircleGradientLayer
//
//  Created by Taurin on 5/8/20.
//  Copyright © 2020 Dinotech. All rights reserved.
//

import UIKit

@objc class GradientView: UIView {
    
    //进度条线条宽度
    @objc public var progressWidth:CGFloat = 10
    @objc public var colors:[Any]? = nil {
        didSet {
            gradientLayer2.colors = colors
            gradientLayer.colors = colors
        }
    }
    
    //进度条轨道颜色
    @objc public var trackColor = UIColor.lightGray
    
    //渐变进度条
    var progressLayer:CAShapeLayer!
    private var gradientLayer:CAGradientLayer!
    private var gradientLayer2:CAGradientLayer!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        gradent(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        gradent(frame: frame)
    }
    
    private func gradent(frame:CGRect) {
        //进度条所在圆的直径
        let progressDiameter:CGFloat = bounds.width
        
        //轨道以及上面进度条的路径（在组件内部水平居中）
        let point = CGPoint(x: progressDiameter * 0.5, y: progressDiameter * 0.5)
        let radius = (progressDiameter - progressWidth) / 2
        let startAngle = -CGFloat.pi * 0.5
        let endAngle = CGFloat.pi * 3 * 0.5
        let path = UIBezierPath(arcCenter: point,
                                radius: radius,
                                startAngle: startAngle,
                                endAngle: endAngle,
                                clockwise: true)
        
        //绘制进度条背景轨道
        let trackLayer = CAShapeLayer()
        trackLayer.frame = bounds
        trackLayer.fillColor = UIColor.clear.cgColor
        //设置轨道颜色
        trackLayer.strokeColor = trackColor.cgColor
        //设置轨道透明度
        trackLayer.opacity = 0.25
        //轨道使用圆角线条
        trackLayer.lineCap = CAShapeLayerLineCap.round
        //轨道线条的宽度
        trackLayer.lineWidth = progressWidth
        //设置轨道路径
        trackLayer.path = path.cgPath
        //将轨道添加到视图层中
        layer.addSublayer(trackLayer)
        
        //绘制进度条
        progressLayer = CAShapeLayer()
        progressLayer.frame = bounds
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = UIColor.black.cgColor
        progressLayer.lineCap = CAShapeLayerLineCap.round
        progressLayer.lineWidth = progressWidth
        progressLayer.path = path.cgPath
        // 进度条默认结束位置是0
        progressLayer.strokeEnd = 0
        // 将进度条添加到视图层中
        layer.addSublayer(progressLayer)
        
        // 绘制左侧的渐变层
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRect(x:0, y:0,
                                      width:frame.width / 2,
                                      height:progressDiameter)
        gradientLayer.locations = [0,1]
        
        //绘制右侧的渐变层
        gradientLayer2 = CAGradientLayer()
        gradientLayer2.frame = CGRect(x:frame.width / 2, y:0,
                                      width:frame.width / 2,
                                      height:progressDiameter)
        gradientLayer2.locations = [0,1]
        
        //用于存放左右两侧的渐变层，并统一添加到视图层中
        let alayer = CALayer()
        alayer.addSublayer(gradientLayer)
        alayer.addSublayer(gradientLayer2)
        layer.addSublayer(alayer)
        
        //将渐变层的遮罩设置为进度条
        alayer.mask = progressLayer
    }
    
    //设置进度（可以设置是否播放动画，以及动画时间）
    @objc public func setPercent(progress:CGFloat, animated:Bool = true) {
        //改变进度条终点，并带有动画效果（如果需要的话）
        CATransaction.begin()
        CATransaction.setAnimationDuration(2)
        CATransaction.setDisableActions(!animated)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name:
            CAMediaTimingFunctionName.easeInEaseOut))
        progressLayer.strokeEnd = progress
        CATransaction.commit()
    }
}
